<?php

namespace App\Http\Controllers\Admin\RoomManager;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;
use Illuminate\Support\Facades\Validator;

class SpecialRoomController extends BaseController {

    /**
     * @api {post} /room/special-room 特殊房间-创建
     * @apiVersion 0.1.0
     * @apiName 房间管理-特殊房间-创建
     * @apiGroup 房间管理
     *
     *
     * @apiParam {number} room_id 房间id 必传
     * @apiParam {string} special_flag_big 大标记 可选
     * @apiParam {string} special_flag_small 小标记 可选
     * @apiParam {string} frame_big 大边框 可选
     * @apiParam {string} frame_small 小边框 可选
     * @apiParam {string} expired_time 过期时间 可选
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                    'room_id' => 'required|numeric|exists:mysql.ln_user_room,room_id',
                    'special_flag_big' => 'filled|string|active_url',
                    'special_flag_small' => 'filled|string|active_url',
                    'frame_big' => 'filled|string|active_url',
                    'frame_small' => 'filled|string|active_url',
                    'expired_time' => 'filled|string|date_format:Y-m-d H:i:s',
                ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $params = $request->input();

        $userRoom = app("model_user_room")->where("room_id", $params['room_id'])->first();
        $params['room_id'] = $userRoom->id;

        $res = app('repo_room');
        $params['add_time'] = time();
        if (!empty($params['expired_time'])) {
            $params['expired_time'] = strtotime($params['expired_time']);
        }

        $rows = $res->addSpecialRoom($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /room/special-room 特殊房间-删除
     * @apiVersion 0.1.0
     * @apiName 房间管理-特殊房间-删除
     * @apiGroup 房间管理
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_room')->delSpecialRoom($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /room/special-room 特殊房间-修改
     * @apiVersion 0.1.0
     * @apiName 房间管理-特殊房间-修改
     * @apiGroup 房间管理
     *
     *
     * @apiParam {number} id
     * @apiParam {number} room_id 房间id 必传
     * @apiParam {string} special_flag_big 大标记 可选
     * @apiParam {string} special_flag_small 小标记 可选
     * @apiParam {string} frame_big 大边框 可选
     * @apiParam {string} frame_small 小边框 可选
     * @apiParam {string} expired_time 过期时间 可选
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|integer',
                'room_id' => 'required|numeric|exists:mysql.ln_user_room,room_id',
                'special_flag_big' => 'filled|string|active_url',
                'special_flag_small' => 'filled|string|active_url',
                'frame_big' => 'filled|string|active_url',
                'frame_small' => 'filled|string|active_url',
                'expired_time' => 'filled|string|date_format:Y-m-d H:i:s',
            ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $id = $request->input('id');

        $res = app('repo_room');

        $params = $request->input();
        $userRoom = app("model_user_room")->where("room_id", $params['room_id'])->first();
        $params['room_id'] = $userRoom->id;
        if (!empty($params['expired_time'])) {
            $params['expired_time'] = strtotime($params['expired_time']);
        }
        $params['special_flag_big']=$params['special_flag_big']??'';
        $params['special_flag_small']=$params['special_flag_small']??'';
        $params['frame_big']=$params['frame_big']??null;
        $params['frame_small']=$params['frame_small']??null;
        $rows = $res->saveSpecialRoom(["id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /room/special-room 特殊房间-列表
     * @apiVersion 0.1.0
     * @apiName 房间管理-特殊房间-列表
     * @apiGroup 房间管理
     *
     * @apiParam {string} keyword 搜索关键字 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "id(序号)": 7,
                "special_flag_big(大标记)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f98db6abb821.webp",
                "special_flag_small(小标记)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f98db8c6ceb3.webp",
                "frame_big(大边框)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f98dab7cc361.webp",
                "frame_small(小边框)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f98db2a02f9f.webp",
                "expired_time(过期时间)": "2020-11-30 00:00:00",
                "room_name(房间名称)": "此房间有BUG找欧工",
                "room_id(房间id)": 1486668,
                "nickname(房主昵称)": "哭",
                "user_code(房主用户编号)": 859187
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'keyword' => 'filled|string',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_room')->getSpecialRoomQueryList($request->input());

        return Response::sendList($list);
    }


}
